package com.jsz.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jsz.dao.PermissionDao;
import com.jsz.entity.PageResult;
import com.jsz.entity.QueryPageBean;
import com.jsz.pojo.Permission;
import com.jsz.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceimpl implements PermissionService {
    @Autowired
    private PermissionDao permissionDao;

    //添加权限数据
    @Override
    public void add(Permission permission) {
        permissionDao.add(permission);
    }

    //权限管理分页查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //获取当前页
        Integer currentPage = queryPageBean.getCurrentPage();
        //获取分页条数
        Integer pageSize = queryPageBean.getPageSize();
        //获取查询条件
        String queryString = queryPageBean.getQueryString();

        //调用分页插件,进行分页操作
        PageHelper.startPage(currentPage, pageSize);
        //将查询条件带入持久层进行数据查询
        Page<Permission> page = permissionDao.findPage(queryString);
        List<Permission> rows = page.getResult();
        long total = page.getTotal();
        return new PageResult(total, rows);
    }

    //根据权限ID查询
    @Override
    public Permission findById(Integer id) {
        return permissionDao.findById(id);
    }

    //修改权限数据
    @Override
    public void update(Permission permission) {
        permissionDao.update(permission);
    }

    //删除数据
    @Override
    public void deleteById(Integer id) {
        //查询中间关系表,看是已经关联角色
        Long count = permissionDao.findRoleAndPermissionById(id);
        if (count > 0) {
            //说明已经关联了对应的角色信息,不允许删除
            throw new RuntimeException("当前检查项已经被引用，不能被删除!");
        }
        //没有关联角色信息
        permissionDao.daleteById(id);
    }

    //查询所有权限信息
    @Override
    public List<Permission> findAll() {
        return permissionDao.findAll();
    }
}
